﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.XSSF.UserModel;
using Web.IBLL;
using Web.Model;

namespace WebAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HouseController : ControllerBase
    {
        public HouseController(IHouseBLL bLL)
        {
            BLL = bLL;
        }

        public IHouseBLL BLL { get; }
        [HttpPost]
        public Task<int> HAdd(HouseClass h)
        {
            return BLL.HAdd(h);
        }
        [HttpGet]
        public dynamic HShow(int Pageindex, int Pagesize, int HNum)
        {
            return BLL.HShow(Pageindex, Pagesize, HNum);
        }
        [HttpPost]
        public Task<int> TAdd(HouseType t)
        {
            return BLL.TAdd(t);
        }
        [HttpGet]
        public List<HouseType> Xia()
        {
            return BLL.Xia();
        }
        [HttpGet]
        public IActionResult DaoChu()
        {
            List<HouseType> list = BLL.Xia();
            XSSFWorkbook wb = new XSSFWorkbook();
            var sheet = wb.CreateSheet("类型表");
            var row = sheet.CreateRow(0);
            int num = 1;
            row.CreateCell(0).SetCellValue("类型编号");
            row.CreateCell(1).SetCellValue("类型名称");
            foreach (var h in list)
            {
                var rows = sheet.CreateRow(num);
                rows.CreateCell(0).SetCellValue(h.HTId);
                rows.CreateCell(1).SetCellValue(h.HTName);

                num++;
            }
            MemoryStream ms = new MemoryStream();
            wb.Write(ms);
            byte[] bytes = ms.ToArray();
            return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "房间类型信息表.xlsx");
        }
    }
}
